# -* - coding: UTF-8 -* -
#import sys
#reload(sys)
#sys.setdefaultencoding('utf8')

import threading,string
from time import sleep
from base_thread import *
import logging

#从外部队列读取消息放入内部对列
class WorkerThread(Thread):  
	def __init__(self,server,queue):  
		Thread.__init__(self) 
		self.task_queue = queue
		self.queue_server = server
		#for i in range (0,1000):
		#	self.task_queue.put('{"command":"queue.Order.test","type":"worker","plan":"at","next_time":1526017782,"interval":null,"times":1,"uuid":"{E31B7575-770F-8696-A6AB-729A0C3D9C73}","data":{"scene":"again","orders_sn":"113003219"},"sign":null}')#
	def on_run(self):
		logging.info("QueueThread Begin")
		try:
			self.queue_server.listen(self.request)
		except Exception as e:
			logging.error('on_run')
			logging.exception(e)
			time.sleep(0.1)
		logging.info("QueueThread End")
			
	def on_stop(self):
		logging.info('queue_server stop')
		self.queue_server.stop()
		
	def request(self, ch, method, props, body):  #回调
		#logging.info("收到消息")#,body.decode()
		try:
			if body:
				self.task_queue.put(body.decode())
		except Exception as e:
			logging.error('request')
			logging.exception(e)
			time.sleep(0.1)		
		

